package org.locker.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaQrcodeService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.locker.common.core.exception.ServiceException;
import org.locker.common.core.utils.MapstructUtils;
import org.locker.common.core.utils.StringUtils;
import org.locker.common.excel.core.ExcelResult;
import org.locker.common.excel.utils.ExcelUtil;
import org.locker.common.mybatis.core.page.PageQuery;
import org.locker.common.redis.utils.RedisUtils;
import org.locker.common.satoken.utils.LoginHelper;
import org.locker.device.dto.ManagerClientQueryDto;
import org.locker.domain.Device;
import org.locker.domain.DeviceGrid;
import org.locker.domain.Position;
import org.locker.domain.bo.BluetoothImportBo;
import org.locker.domain.bo.DeviceBo;
import org.locker.domain.bo.SerialImportBo;
import org.locker.domain.dto.GridFeesConfig;
import org.locker.domain.enums.*;
import org.locker.domain.vo.*;
import org.locker.mapper.DeviceGridMapper;
import org.locker.mapper.DeviceMapper;
import org.locker.mapper.PositionMapper;
import org.locker.netty.MachineService;
import org.locker.service.IDeviceService;
import org.locker.service.IOrderService;
import org.locker.system.domain.vo.SysOssVo;
import org.locker.system.service.ISysOssService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.math.BigDecimal;
import java.time.Duration;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 设备信息Service业务层处理
 *
 * @author winnie
 * @date 2023-10-11
 */
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements IDeviceService {

    private final DeviceMapper baseMapper;
    private final PositionMapper positionMapper;
    private final DeviceGridMapper deviceGridMapper;
    private final WxMaService wxMaService;
    private final WxMpService wxMpService;
    private final ISysOssService ossService;
    private final IOrderService orderService;

    private final MachineService machineService;
    private static final String BASH_PATH = "/pages/serve/bagCheck/index?deviceNo={}";

    /**
     * 查询设备信息
     */
    @Override
    public DeviceVo queryById(Long deviceId) {
        DeviceVo deviceVo = baseMapper.selectVoById(deviceId);
        if (ObjectUtil.isNotNull(deviceVo)) {
            List<Position> positions = positionMapper.selectList();
            Map<Long, String> positionMap = positions.stream()
                .collect(Collectors.toMap(
                    position -> position.getPositionId(),
                    position -> position.getName()));
            deviceVo.setPositionName(positionMap.getOrDefault(deviceVo.getPositionId(), ""));
        }
        return deviceVo;
    }

    /**
     * 查询设备信息列表
     */
    @Override
    public Page<DeviceVo> queryPageList(DeviceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Device> lqw = buildQueryWrapper(bo);
        Page<DeviceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        List<Position> positions = positionMapper.selectList();
        List<DeviceVo> records = result.getRecords();
        if (CollUtil.isNotEmpty(positions) && CollUtil.isNotEmpty(records)) {
            Map<Long, String> positionMap = positions.stream()
                .collect(Collectors.toMap(
                    position -> position.getPositionId(),
                    position -> position.getName()));

            records.stream().forEach(e -> e.setPositionName(positionMap.getOrDefault(e.getPositionId(), "")));
        }
        return result;
    }

    /**
     * 查询设备信息列表
     */
    @Override
    public List<DeviceVo> queryList(DeviceBo bo) {
        LambdaQueryWrapper<Device> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public Page<DeviceVo> listByManagerClient(ManagerClientQueryDto dto, PageQuery pageQuery) {
        Page<DeviceVo> deviceVoPage = baseMapper.listByManagerClient(pageQuery.build(), dto);
        Map<String, List<OrderDetailVo>> map = new HashMap<>();
        if (dto.getIsQueryOrder()) {
            //处理今日订单信息
            Date date = DateUtil.date();
            String day = DateUtil.format(date, "yyyy-MM-dd");
            dto.setDay(day);
            pageQuery.setPageSize(Integer.MAX_VALUE);
            Page<OrderDetailVo> orderDetailVoPage = orderService.queryOrderByDay(dto, pageQuery);
            List<OrderDetailVo> records = orderDetailVoPage.getRecords();
            if (CollUtil.isNotEmpty(records)) {
                //根据设备分组
                map = records.stream().collect(Collectors.groupingBy(OrderDetailVo::getDeviceNo));
            }
        }
        List<DeviceVo> records = deviceVoPage.getRecords();
        for (DeviceVo record : records) {
            Long unUsedCount = deviceGridMapper.selectCount(Wrappers.lambdaQuery(DeviceGrid.class)
                .eq(DeviceGrid::getGridType, "common")
                .eq(DeviceGrid::getDeviceId, record.getDeviceId())
                .eq(DeviceGrid::getStatus, DeviceGridStatusEnum.unlock.getCode()));
            record.setUnUsedNums(unUsedCount);
            //设置订单数据
            String deviceNo = record.getDeviceNo();
            List<OrderDetailVo> orderDetailVos = map.get(deviceNo);
            if (CollUtil.isNotEmpty(orderDetailVos)) {
                record.setTodayOrderNumber(orderDetailVos.size());
                BigDecimal bigDecimal = orderDetailVos.stream().map(OrderDetailVo::getAmount)
                    .filter(Objects::nonNull)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
                record.setTodayAmount(bigDecimal);
            }

        }

        return deviceVoPage;
    }

    private LambdaQueryWrapper<Device> buildQueryWrapper(DeviceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Device> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceNo()), Device::getDeviceNo, bo.getDeviceNo());
        lqw.eq(StringUtils.isNotBlank(bo.getDevicePwd()), Device::getDevicePwd, bo.getDevicePwd());
        lqw.eq(bo.getPositionId() != null, Device::getPositionId, bo.getPositionId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), Device::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getDescribe()), Device::getDescribe, bo.getDescribe());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceLocation()), Device::getDeviceLocation, bo.getDeviceLocation());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), Device::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getChannel()), Device::getChannel, bo.getChannel());
        lqw.eq(bo.getConnectTime() != null, Device::getConnectTime, bo.getConnectTime());
        lqw.eq(bo.getLossTime() != null, Device::getLossTime, bo.getLossTime());
        lqw.eq(bo.getGardNum() != null, Device::getGardNum, bo.getGardNum());
        lqw.eq(StringUtils.isNotBlank(bo.getAdminUser()), Device::getAdminUser, bo.getAdminUser());
        lqw.eq(StringUtils.isNotBlank(bo.getAdminPwd()), Device::getAdminPwd, bo.getAdminPwd());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceImei()), Device::getDeviceImei, bo.getDeviceImei());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceCardNo()), Device::getDeviceCardNo, bo.getDeviceCardNo());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceIccid()), Device::getDeviceIccid, bo.getDeviceIccid());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceType()), Device::getDeviceType, bo.getDeviceType());
        lqw.eq(StringUtils.isNotBlank(bo.getFeeType()), Device::getFeeType, bo.getFeeType());
        return lqw;
    }

    /**
     * 新增设备信息
     */
    @Override
    public Boolean insertByBo(DeviceBo bo) {
        Device add = MapstructUtils.convert(bo, Device.class);
        validEntityBeforeSave(add);
        //自动生成编号与密码
        add.setDeviceNo(IdUtil.getSnowflake().nextIdStr());
        add.setDevicePwd(RandomUtil.randomNumbers(8));
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDeviceId(add.getDeviceId());
            createQrcode(add.getDeviceNo());
            createMpQrcode(add.getDeviceNo());
        }
        return flag;
    }

    /**
     * 修改设备信息
     */
    @Override
    public Boolean updateByBo(DeviceBo bo) {
        Device update = MapstructUtils.convert(bo, Device.class);
        //编号和密码不修改
        update.setDeviceNo(null);
        update.setDevicePwd(null);
        validEntityBeforeSave(update);

        if (bo.getStatus().equals(DeviceStatusEnum.online.getCode())) {
            machineService.deviceCommonInfoSync(bo.getDeviceNo());
        }

        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Device entity) {
        Long deviceId = entity.getDeviceId();
        String deviceNo = entity.getDeviceNo();
        LambdaQueryWrapper<Device> queryWrapper = Wrappers.<Device>lambdaQuery().eq(Device::getDeviceNo, deviceNo);
        if (ObjectUtil.isNotNull(deviceId)) {
            queryWrapper.ne(Device::getDeviceId, deviceId);
        }
        long count = this.count(queryWrapper);
        if (count > 0) {
            throw new ServiceException(StrUtil.format("该设备号:{}已存在!", deviceNo));
        }

    }

    /**
     * 批量删除设备信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Device queryByDeviceNo(String deviceNo) {
        return baseMapper.selectOne(Wrappers.lambdaQuery(Device.class).eq(Device::getDeviceNo, deviceNo));
    }

    @Override
    public DeviceAggregatorInfoVo getDeviceAggregatorInfo(String deviceNo) {
        return getDeviceAggregatorInfo(deviceNo, null);
    }

    @Override
    public DeviceAggregatorInfoVo getDeviceAggregatorInfo(String deviceNo, Long deviceGridId) {
        DeviceAggregatorInfoVo aggregatorInfo = new DeviceAggregatorInfoVo();
        //设备信息
        Device device = this.getOne(Wrappers.<Device>lambdaQuery()
            .select(Device::getDeviceId, Device::getDeviceNo, Device::getDeviceType, Device::getName,
                Device::getGardNum, Device::getStatus, Device::getPositionId, Device::getFeeConfig, Device::getFeeType, Device::getDepositAmount)
            .eq(Device::getDeviceNo, deviceNo));
        aggregatorInfo.setDevice(device);

        //格子信息
        Long deviceId = device.getDeviceId();
        List<DeviceGridVo> deviceGridVos = deviceGridMapper.selectVoList(
            Wrappers.<DeviceGrid>lambdaQuery()
                .eq(DeviceGrid::getDeviceId, deviceId)
                //common 的客户才能使用
                .eq(DeviceGrid::getGridType, "common")
                .eq(ObjectUtil.isNotNull(deviceGridId), DeviceGrid::getDeviceGridId, deviceGridId));

        //处理费用信息
        String feeType = device.getFeeType();
        if (!PositionTypeEnum.free.name().equals(feeType)) {
            //处理收费逻辑
            String feeConfig = device.getFeeConfig();
            List<GridFeesConfig> gridFees = JSONUtil.toList(feeConfig, GridFeesConfig.class);
            //按次
            if (PositionTypeEnum.times.name().equals(feeType)) {
                Map<SpecCodeEnum, BigDecimal> timesMap = gridFees.stream()
                    .collect(Collectors.toMap(GridFeesConfig::getSpecCode, GridFeesConfig::getTimesPrice));
                for (DeviceGridVo vo : deviceGridVos) {
                    vo.setDepositAmount(device.getDepositAmount());
                    GridFeesConfig gridFeesConfig = gridFees.stream()
                        .filter(p -> p.getSpecCode().name().equals(vo.getSpecCode()))
                        .findFirst()
                        .orElse(null);
                    vo.setUnitPriceAmount(gridFeesConfig.getTimesPrice());
                    vo.setMaxAmount(gridFeesConfig.getTimesPrice());
                    vo.setBillingType(feeType);
                }
            }

            //按小时
            if (PositionTypeEnum.hour.name().equals(feeType)) {
                for (DeviceGridVo vo : deviceGridVos) {
                    vo.setDepositAmount(device.getDepositAmount());
                    GridFeesConfig gridFeesConfig = gridFees.stream()
                        .filter(p -> p.getSpecCode().name().equals(vo.getSpecCode()))
                        .findFirst()
                        .orElse(null);
                    vo.setUnitPriceAmount(gridFeesConfig.getHourPrice());
                    vo.setMaxAmount(gridFeesConfig.getMaxHourPrice());
                    vo.setBillingType(feeType);
                }
            }
        } else {
            for (DeviceGridVo vo : deviceGridVos) {
                vo.setDepositAmount(device.getDepositAmount());
                vo.setBillingType(feeType);
            }
        }

        //默认选中一个给用户，用户后面可自己修改

        if (deviceGridId == null && CollectionUtils.isNotEmpty(deviceGridVos)) {
            List<DeviceGridVo> gridVos = deviceGridVos.stream().filter(a -> a.getStatus().equals(DeviceGridStatusEnum.unlock.getCode())).collect(Collectors.toList());
            int size = gridVos.size();
            DeviceGridVo selectGrid = null;
            if (size > 0) {
                for (int i = 0; i < size; i++) {
                    int index = RandomUtil.randomInt(0, size);
                    selectGrid = gridVos.get(index);
//                    如果不存在则设置 并返回 true 如果存在则返回 false
                    boolean ifAbsent = RedisUtils.setObjectIfAbsent(deviceNo + ":" + selectGrid.getDeviceGridId(), LoginHelper.getUserId() == null ? 0 : LoginHelper.getUserId(), Duration.ofSeconds(30));
                    if (ifAbsent) {
                        break;
                    }
                }
                aggregatorInfo.setDefaultSelect(selectGrid);
            }
        }


        aggregatorInfo.setDeviceGridList(deviceGridVos);
        return aggregatorInfo;
    }


    @Override
    public List<DeviceVo> queryByPositionId(Long positionId) {
        LambdaQueryWrapper<Device> queryWrapper = Wrappers.lambdaQuery(Device.class).eq(Device::getPositionId, positionId);
        List<Device> devices = this.list(queryWrapper);
        return MapstructUtils.convert(devices, DeviceVo.class);
    }

    @Override
    public void createQrcode(String deviceNo) {
        Device one = this.getOne(Wrappers.<Device>lambdaQuery().eq(Device::getDeviceNo, deviceNo));
        if (ObjectUtil.isNull(one)) {
            return;
        }
        String path = StrUtil.format("/pages/serve/bagCheck/index?deviceNo={}", deviceNo);
        WxMaQrcodeService qrcodeService = wxMaService.getQrcodeService();
        try {
            //todo: 要根据环境进行切换
            File qrcode = qrcodeService.createQrcode(path, 430);
            //File qrcode = qrcodeService.createWxaCode(path, "trial",430,true, new WxMaCodeLineColor(),false);
            //保存
            SysOssVo upload = ossService.upload(qrcode);
            this.update(Wrappers.<Device>lambdaUpdate().set(Device::getDeviceQrCode, upload.getUrl()).eq(Device::getDeviceNo, deviceNo));
        } catch (Exception e) {
            log.error("生成二维码失败!", e);
        }
    }


    @Override
    public void createMpQrcode(String deviceNo) {
        Device one = this.getOne(Wrappers.<Device>lambdaQuery().eq(Device::getDeviceNo, deviceNo));
        if (ObjectUtil.isNull(one)) {
            return;
        }
        PositionVo positionVo = positionMapper.selectVoById(one.getPositionId());
        if (StrUtil.isNotBlank(positionVo.getBindMpAppId())) {
            WxMpService mpService = this.wxMpService.switchoverTo(positionVo.getBindMpAppId());
            try {
                WxMpQrCodeTicket wxMpQrCodeTicket = null;
                wxMpQrCodeTicket = mpService.getQrcodeService().qrCodeCreateLastTicket(wxMaService.getWxMaConfig().getAppid() + "@" + deviceNo);
                File file = mpService.getQrcodeService().qrCodePicture(wxMpQrCodeTicket);
                //File qrcode = qrcodeService.createWxaCode(path, "trial",430,true, new WxMaCodeLineColor(),false);
                //保存
                SysOssVo upload = ossService.upload(file);
                this.update(Wrappers.<Device>lambdaUpdate().set(Device::getDeviceMpQrCode, upload.getUrl()).eq(Device::getDeviceNo, deviceNo));
            } catch (Exception e) {
                log.error("生成二维码失败!", e);
            }
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importGrid(MultipartFile file, String deviceNo) {
        Device device = queryByDeviceNo(deviceNo);
        if (ObjectUtil.isNull(device)) {
            throw new ServiceException(StrUtil.format("设备: {}不存在!", deviceNo));
        }
        List<DeviceGrid> deviceGrids = null;
        if (DeviceTypeEnum.BLUETOOTH.getCode().equals(device.getDeviceType())) {
            deviceGrids = importBluetooth(file, deviceNo, device);
        } else if (DeviceTypeEnum.SERIAL.getCode().equals(device.getDeviceType())) {
            deviceGrids = importSerial(file, deviceNo, device);
        } else {
            return;
        }

        if (CollUtil.isNotEmpty(deviceGrids)) {
            deviceGridMapper.insertBatch(deviceGrids);
            statisticsGridNumber(deviceNo);
        }
    }


    private List<DeviceGrid> importSerial(MultipartFile file, String deviceNo, Device device) {
        ExcelResult<SerialImportBo> result;
        try {
            result = ExcelUtil.importExcel(file.getInputStream(), SerialImportBo.class, true);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new ServiceException("导入异常,请联系管理员! 异常信息: " + e.getMessage());
        }
        List<SerialImportBo> list = result.getList();
        List<String> numList = list.stream().map(SerialImportBo::getNumber).collect(Collectors.toList());
        Long aLong = deviceGridMapper.selectCount(Wrappers.lambdaQuery(DeviceGrid.class)
            .eq(DeviceGrid::getDeviceNo, deviceNo).in(DeviceGrid::getNumber, numList));
        if (aLong > 0) {
            throw new ServiceException("导入异常,导入的格子编号与已存在的格子编号重复!请认真核对后再导入!");
        }
        List<DeviceGrid> deviceGrids = list.stream()
            .map(e -> {
                DeviceGrid deviceGrid = new DeviceGrid();
                deviceGrid.setDeviceId(device.getDeviceId());
                deviceGrid.setPositionId(device.getPositionId());
                deviceGrid.setDeviceNo(deviceNo);
                deviceGrid.setName(e.getNumber());
                deviceGrid.setNumber(e.getNumber());
                deviceGrid.setSerialNo(e.getSerialNo());
                deviceGrid.setSerialIndex(e.getSerialIndex());
                deviceGrid.setGridType("common");
                deviceGrid.setStatus(DeviceGridStatusEnum.unlock.getCode());
                deviceGrid.setSpecCode(SpecCodeEnum.getCodeByDesc(e.getSpecCode()));
                return deviceGrid;
            }).collect(Collectors.toList());
        return deviceGrids;
    }

    private List<DeviceGrid> importBluetooth(MultipartFile file, String deviceNo, Device device) {
        ExcelResult<BluetoothImportBo> result;
        try {
            result = ExcelUtil.importExcel(file.getInputStream(), BluetoothImportBo.class, true);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new ServiceException("导入异常,请联系管理员! 异常信息: " + e.getMessage());
        }
        List<BluetoothImportBo> list = result.getList();
        List<String> numList = list.stream().map(BluetoothImportBo::getNumber).collect(Collectors.toList());
        Long aLong = deviceGridMapper.selectCount(Wrappers.lambdaQuery(DeviceGrid.class)
            .eq(DeviceGrid::getDeviceNo, deviceNo).in(DeviceGrid::getNumber, numList));
        if (aLong > 0) {
            throw new ServiceException("导入异常,导入的格子编号与已存在的格子编号重复!请认真核对后再导入!");
        }

        List<String> macNoList = list.stream().map(BluetoothImportBo::getMacNo).collect(Collectors.toList());
        Long mLong = deviceGridMapper.selectCount(Wrappers.lambdaQuery(DeviceGrid.class)
            .eq(DeviceGrid::getDeviceNo, deviceNo).in(DeviceGrid::getMacNo, macNoList));
        if (mLong > 0) {
            throw new ServiceException("导入异常,导入的蓝牙编号与已存在的蓝牙编号重复!请认真核对后再导入!");
        }
        List<DeviceGrid> deviceGrids = list.stream()
            .map(e -> {
                DeviceGrid deviceGrid = new DeviceGrid();
                deviceGrid.setDeviceId(device.getDeviceId());
                deviceGrid.setPositionId(device.getPositionId());
                deviceGrid.setDeviceNo(deviceNo);
                deviceGrid.setName(e.getNumber());
                deviceGrid.setNumber(e.getNumber());
                deviceGrid.setMacNo(e.getMacNo());
                deviceGrid.setGridType("common");
                deviceGrid.setStatus(DeviceGridStatusEnum.unlock.getCode());
                deviceGrid.setSpecCode(SpecCodeEnum.getCodeByDesc(e.getSpecCode()));
                return deviceGrid;
            }).collect(Collectors.toList());
        return deviceGrids;
    }

    /**
     * 统计格子数量
     *
     * @param deviceNo 设备号
     */
    private void statisticsGridNumber(String deviceNo) {
        long count = deviceGridMapper.selectCount(Wrappers.<DeviceGrid>lambdaQuery().eq(DeviceGrid::getDeviceNo, deviceNo));
        this.update(Wrappers.<Device>lambdaUpdate()
            .set(Device::getGardNum, count).eq(Device::getDeviceNo, deviceNo));
    }


    @Override
    public void updateMpQrCodeByPostion(Position postion) {
        if (StrUtil.isNotBlank(postion.getBindMpAppId())) {
            List<Device> deviceList = lambdaQuery().eq(Device::getPositionId, postion.getPositionId()).list();
            for (Device device : deviceList) {
                if (StrUtil.isBlank(device.getDeviceMpQrCode())) {
                    WxMpService mpService = this.wxMpService.switchoverTo(postion.getBindMpAppId());
                    try {
                        WxMpQrCodeTicket wxMpQrCodeTicket = null;
                        wxMpQrCodeTicket = mpService.getQrcodeService().qrCodeCreateLastTicket(wxMaService.getWxMaConfig().getAppid() + "@" + device.getDeviceNo());
                        File file = mpService.getQrcodeService().qrCodePicture(wxMpQrCodeTicket);
                        //File qrcode = qrcodeService.createWxaCode(path, "trial",430,true, new WxMaCodeLineColor(),false);
                        //保存
                        SysOssVo upload = ossService.upload(file);
                        this.update(Wrappers.<Device>lambdaUpdate().set(Device::getDeviceMpQrCode, upload.getUrl()).eq(Device::getDeviceNo, device.getDeviceNo()));
                    } catch (Exception e) {
                        log.error("生成二维码失败!", e);
                        e.printStackTrace();
                    }
                }
            }

        }

    }
}
